package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.execption.BizException;
import com.huiminpay.merchant.common.SecurityUtil;
import com.huiminpay.merchant.convert.MerchantConvert;
import com.huiminpay.merchant.vo.PayChannelParamVo;
import com.huiminpay.transaction.api.dto.PayChannelDTO;
import com.huiminpay.transaction.api.dto.PayChannelParamDTO;
import com.huiminpay.transaction.api.dto.PlatformChannelDTO;
import com.huiminpay.transaction.api.service.IPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(value = "支付渠道参数操作相关",tags = "支付操作相关",description = "支付操作相关1")
public class PayController {
    @Reference
    IPayService iPayService;

    @ApiOperation("查询所有服务类型")
    @GetMapping("my/platform-channels")
    public List<PlatformChannelDTO> finaAll(){
        log.info("查询所有服务类型");
        return iPayService.finaAllPlatformchannel();
    }

    @ApiOperation("开启服务")
    @PostMapping(value = "my/apps/{appId}/platform-channels")
    @ApiImplicitParam(name = "platformchannel",value = "服务类型编码",example = "huimi_b2c")
    public Integer openService(@PathVariable String appId, @RequestParam(required = true) String platformChannelCodes ){
        log.info("开启服务");
        //参数非空校验appId platformchannel
        if (StringUtils.isEmpty(appId)){
            throw new RuntimeException("应用id不能为空");
        }
        Integer i =iPayService.openAppService(appId,platformChannelCodes);
        return i;
    }
    @ApiOperation("查询服务是否已经开启")
    @GetMapping(value = "my/apps/{appId}/platformchannels")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用id",example = ""),
            @ApiImplicitParam(name = "platformchannel",value = "服务类型编码",example = "huimi_b2c")
    })
    public Integer queryOpenService(@RequestParam String appId, @RequestParam(required = true) String platformchannel ){
        return iPayService.queryOpenService(appId,platformchannel);
    }


    @ApiOperation("查询应用是否绑定了某个服务类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用appId", required = true, dataType =
                    "String", paramType = "query"),
            @ApiImplicitParam(name = "platformChannel", value = "服务类型", required = true, dataType =
                    "String", paramType = "query")
    })
    @GetMapping("/my/merchants/apps/platformchannels")
    public int queryAppBindPlatformChannel(@RequestParam String appId, @RequestParam String
            platformChannel){

        return iPayService.queryAppBindPlatformChannel(appId,platformChannel);
    }

    @ApiOperation("根据平台支付渠道获取原始支付渠道")
    @GetMapping(value = "my/pay-channels/platform-channel/{platformchannelCode}")
    @ApiImplicitParam(name = "platformchannelCode",value = "平台支付渠道编码",example = "huimi_b2c")
    public List<PayChannelDTO> findPlatformChannels(@PathVariable String platformchannelCode) {
        log.info("根据平台支付渠道获取原始支付渠道");
        //非空校验
        if (StringUtils.isEmpty(platformchannelCode)) {
            throw new BizException(CommonErrorCode.E_200240);
        }
        //查询

        return iPayService.findPlatformChannels(platformchannelCode);
    }
    @ApiOperation("保存支付渠道参数")
    @PostMapping(value = "my/pay-channel-params")
    @ApiImplicitParam(name = "payChannelParamDTO",value = "平台支付渠道",dataType ="PayChannelParamDTO" )
    public PayChannelParamDTO savePayChannelParam(@RequestBody PayChannelParamVo payChannelParamVo) {
       //非空校验
        if (StringUtils.isEmpty(payChannelParamVo)) {
            throw new BizException(CommonErrorCode.E_200241);
        }
        //如果用vo需要对象转换，等会写
        PayChannelParamDTO payChannelParamDTO = MerchantConvert.INSTANCE.pVodaoDTo(payChannelParamVo);
        //保存或者更新数据库
        log.info("保存支付渠道参数");

        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParamDTO.setMerchantId(merchantId);
        return iPayService.savePayChannelParam(payChannelParamDTO);
    }
    @ApiOperation("获取某应用下平台支付渠道所包含的原始支付参数列表")
    @GetMapping(value = "my/pay-channel-params/apps/{appId}/platform-channels/{platformchannel}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用ID",example = "8be5726c19d043a4beedb0b14e5cae25"),
            @ApiImplicitParam(name = "platformchannel",value = "平台支付渠道编码",example = "huimin_b2c")
    })
    public List<PayChannelParamDTO> queryPlatformChannelPayParams(@PathVariable  String appId,@PathVariable String platformchannel){
        //非空校验

        //数据查询

        return iPayService.queryPlatformChannelPayParams(appId,platformchannel);

    }
    @ApiOperation("获取某应用下平台支付渠道所包含的原始支付参数列表")
    @GetMapping(value = "my/pay-channel-params/apps/{appId}/platform-channels/{platformchannel}/pay-channels/{payChannel}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value = "应用ID",example = "8be5726c19d043a4beedb0b14e5cae25"),
            @ApiImplicitParam(name = "platformchannel",value = "平台支付渠道编码",example = "huimin_b2c"),
            @ApiImplicitParam(name = "payChannel",value = "支付渠道编码",example = "WX_MICROPAY"),

    })
    public PayChannelParamDTO queryPlatformChannelPayParams(@PathVariable  String appId,
           @PathVariable String platformchannel,@PathVariable String payChannel){
        //非空校验

        //数据查询

        return iPayService.queryPlatformChannelPayParam(appId,platformchannel,payChannel);

    }

}
